package cn.com.connext.scrm.repository.master;

import cn.com.connext.scrm.domain.master.Prize;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 2 * @Author: shawn
 * 3 * @Date: 2020/8/11 4:14 下午
 * 4
 */
@Repository
public interface PrizeRepository extends JpaRepository<Prize, Integer>, JpaSpecificationExecutor {

    @Query(value = "select count(1) from prize where union_id = ?1 ", nativeQuery = true)
    Integer findIsPrize(String unionId);

    @Query(value = "select good_id from prize where id = ?1 ", nativeQuery = true)
    Integer findGoodIdByPrizeId(Integer prizeId);

    Prize findByUnionId(String unionId);

    List<Prize> findByUnionIdAndActivityId(String unionId,String activityId);

    @Query(value = "select * from prize where union_id=?1 order by create_time desc",nativeQuery = true)
    List<Prize> findUnionId(String unionId);

    List<Prize> findByUnionIdAndGoodId(String unionId, Integer goodId);

    Prize findByUnionIdAndActivityIdAndGoodId(String unionId, String activityId, Integer goodId);

    @Modifying
    @Query(value= "update prize p set p.be_scan_code=?1 where  p.union_id=?2 and p.id=?3",nativeQuery = true)
    int updatePrizeByHx(String beScanCode,String unionId,int prizeId);

    @Query(value = "select * from prize where union_id=?1 and id=?2 ",nativeQuery = true)
    Prize findByUnionIdAndId(String unionId, int id);
}
